package com.ice.work.moment.ticket.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.honeybee.cloud.framework.base.exception.business.BaseBizException;
import com.honeybee.cloud.framework.common.aop.Log;
import com.honeybee.cloud.framework.common.page.Page;
import com.honeybee.cloud.framework.common.response.ResponseEntity;
import com.honeybee.cloud.framework.mybatis.page.PageMapper;
import com.ice.work.moment.facade.ticket.controller.IUserController;
import com.ice.work.moment.facade.ticket.dto.CookieDTO;
import com.ice.work.moment.facade.ticket.dto.PassengerDTO;
import com.ice.work.moment.facade.ticket.mapper.ParamConvertMapper;
import com.ice.work.moment.facade.ticket.param.LoginParam;
import com.ice.work.moment.facade.ticket.param.QueryPassengerParam;
import com.ice.work.moment.facade.ticket.request.LoginRequest;
import com.ice.work.moment.ticket.service.ICookieService;
import com.ice.work.moment.ticket.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Description user
 * @Author DELL
 * @Create 2019-05-17
 * @Since 1.0.0
 */
@RestController
@SuppressWarnings("unused")
public class UserController implements IUserController {
    @Autowired
    private ICookieService cookieService;
    @Autowired
    private IUserService userService;

    @Override
    @Log(name = "用户登录")
    public ResponseEntity<String> login(LoginRequest request) {
        LoginParam loginParam = ParamConvertMapper.INSTANCE.toParam(request);
        String appKey = userService.login(loginParam, request.getQrLogin());
        return ResponseEntity.ok(appKey);
    }

    @Override
    public ResponseEntity<Void> out(String appKey) {
        CookieDTO cookieDTO = cookieService.findByAppKey(appKey);
        if (Objects.isNull(cookieDTO) || StringUtils.isEmpty(cookieDTO.getAppKey())) {
            return ResponseEntity.ok();
        }
        userService.out(cookieDTO);
        return ResponseEntity.ok();
    }

    @Override
    public ResponseEntity<Page<PassengerDTO>> queryPassengers(Long page, Long size, String appKey) throws BaseBizException {
        IPage iPage = PageMapper.INSTANCE.pageInfo(page, size);
        QueryPassengerParam param = ParamConvertMapper.INSTANCE.toParam(iPage);
        CookieDTO cookieDTO = cookieService.findByAppKey(appKey);
        Page<PassengerDTO> dtoPage = userService.queryPassengers(param, cookieDTO);
        return ResponseEntity.ok(dtoPage);
    }

}
